import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hotpot/basic/app_colors.dart';
import 'package:hotpot/odoo/odoo_helper.dart';
import 'package:hotpot/route/route_ext.dart';
import 'package:hotpot/route/routes.dart';
import 'package:hotpot/utils/storage_utils.dart';
import 'package:hotpot/widgets/widget_ext.dart';
import 'package:hotpot/widgets/z_container.dart';

class ServerPage extends StatefulWidget {
  const ServerPage({super.key});

  @override
  State<ServerPage> createState() => _ServerPageState();
}

class _ServerPageState extends State<ServerPage> {
  var controller = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (kDebugMode) controller.text = 'https://sushimoya.intpv.es';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.baColor,
      body: SingleChildScrollView(
        child: Column(
          children: [
            54.hSizedBox,
            'images/img_logo.png'.assetImage(width: 141, height: 40),
            67.hSizedBox,
            TextField(
              controller: controller,
              decoration: InputDecoration(
                labelText: 'Introducir la URL de su servidor ',
                labelStyle: TextStyle(color: const Color(0xFFB2B2B2), fontSize: 8.w),
              ),
              style: TextStyle(color: const Color(0xFFB2B2B2), fontSize: 8.w),
            ).sizedBox(width: 298),
            35.hSizedBox,
            ZContainer(
              alignment: Alignment.center,
              radius: 18,
              width: 94,
              height: 35,
              color: const Color(0xFF22CE67),
              child: 'INICIO'.text(size: 10, color: Colors.white, bold: true),
            ).gestureDetector(onTap: _login),
          ],
        ),
      ).align(Alignment.topCenter),
    );
  }

  void _login() async {
    var server = controller.text;
    if (server.isEmpty) {
      showToast('请输入服务器');
      return;
    }
    StorageUtils.saveServer(server);
    await OdooHelper.initClient(server);
    Routes.loginData.popAndPushNamed();
  }
}
